package com.wiki.gds.member.provider.security.oauth2.core.authenticator;

import com.wiki.gds.base.common.exception.BizExceptionEnum;
import com.wiki.gds.base.common.exception.BusinessException;
import com.wiki.gds.base.security.common.integration.IntegrationAuthentication;
import com.wiki.gds.base.security.common.integration.authenticator.AbstractPreparableIntegrationAuthenticator;
import com.wiki.gds.base.security.common.model.SecurityMember;
import com.wiki.gds.member.facade.member.model.Member;
import com.wiki.gds.member.provider.modular.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 默认登录处理
 * @author WIKI
 * @date 2018-3-31
 **/
@Component
@Primary
public class UsernamePasswordAuthenticator extends AbstractPreparableIntegrationAuthenticator {

    @Autowired
    private MemberService memberService;

    @Override
    public SecurityMember authenticate(IntegrationAuthentication integrationAuthentication) {

            Member member=new Member();
            member.setUsername(integrationAuthentication.getUsername());
            member = memberService.getBySelf(member);
            if(member==null){
                throw new BusinessException(BizExceptionEnum.NO_THIS_USER);
            }
            SecurityMember securityMember=new SecurityMember(member.getUsername(),member.getPassword());
            securityMember.setId(member.getId());
            return securityMember;
    }

    @Override
    public void prepare(IntegrationAuthentication integrationAuthentication) {

    }

    @Override
    public boolean support(IntegrationAuthentication integrationAuthentication) {
        return StringUtils.isEmpty(integrationAuthentication.getAuthType());
    }
}
